<template>
  <div class="login-body">
    <div class="header">
      <div class="logo">
        <img src="/logo.png" alt="" />
        <h2>ZeroMS</h2>
      </div>
      <div class="inner-header flex">
        <el-card class="login-box" shadow="hover">
          <h2>ZeroMS</h2>
          <el-form :label-position="labelPosition" :model="formLabelAlign" style="max-width: 98%">
            <el-form-item :error="errorMessage.username">
              <el-input clearable size="large" :prefix-icon="User" v-model="formLabelAlign.username" />
            </el-form-item>
            <el-form-item :error="errorMessage.password">
              <el-input clearable size="large" :prefix-icon="Unlock" show-password type="password"
                v-model="formLabelAlign.password" />
            </el-form-item>
            <el-button type="primary" plain @click="login" v-show="noLogin">登录</el-button>
            <el-button type="primary" plain loading v-show="isLogin">登录中...</el-button>
          </el-form>
        </el-card>
      </div>

      <!--Waves Container-->
      <svg class="waves" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
        viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto">
        <defs>
          <path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z" />
        </defs>
        <g class="parallax">
          <use xlink:href="#gentle-wave" x="48" y="0" fill="rgba(255,255,255,0.7)" />
          <use xlink:href="#gentle-wave" x="48" y="3" fill="rgba(255,255,255,0.5)" />
          <use xlink:href="#gentle-wave" x="48" y="5" fill="rgba(255,255,255,0.3)" />
          <use xlink:href="#gentle-wave" x="48" y="7" fill="#fff" />
        </g>
      </svg>
      <div class="footer">@冒险岛管理系统</div>
      <!--Waves end-->
    </div>
  </div>
</template>
<script>
export default {
    name: 'loginIndex',
    title: '登录页',
    order: 2,
    haveThis: true
}
</script>
<script setup>
import { ref, reactive, onMounted, onUnmounted } from "vue";
import { Unlock, User } from "@element-plus/icons-vue";
import { openMessage, routGo } from "../utils/help.js";
import { moneyDictionary, userLogin } from "@/api/user";

const isLogin = ref(false);
const noLogin = ref(true);
const errorMessage = ref({
  username: "",
  password: "",
});
const labelPosition = ref("left");
const formLabelAlign = reactive({
  username: "",
  password: "",
});

const dictionary = async () => {
  let { code, data } = await moneyDictionary();
  if (code === 1) {
    localStorage.setItem("moneyName", JSON.stringify(data));
  }
};

const login = async () => {
  noLogin.value = false;
  isLogin.value = true;
  // 验证输入
  if (!formLabelAlign.username || !formLabelAlign.password) {
    errorMessage.value.username = formLabelAlign.username ? "" : "账号不能为空";
    errorMessage.value.password = formLabelAlign.password ? "" : "密码不能为空";
    resetLoginStatus();
    return;
  }
  try {
    // 执行登录请求
    const { code, data, message } = await userLogin(formLabelAlign.username, formLabelAlign.password);
    if (code === 1) {
      // 成功登录，保存用户信息和token
      localStorage.setItem("user", JSON.stringify({ name: data.name, id: data.id, username: data.username }));
      localStorage.setItem("token", data.token);
      openMessage(1, message);
      routGo("home");
    } else {
      // 登录失败，显示错误消息
      openMessage(2, message);
    }
  } catch (e) {
    // 网络或其他错误
    openMessage(2, "系统无响应，请检查网络");
  } finally {
    // 重置登录状态
    resetLoginStatus();
  }
};

const resetLoginStatus = () => {
  errorMessage.value.username = "";
  errorMessage.value.password = "";
  noLogin.value = true;
  isLogin.value = false;
};

const keyDown = (e) => {
  if (e.keyCode === 13) {
    login();
  }
};

onMounted(() => {
  dictionary();
  window.addEventListener("keydown", keyDown);
});

onUnmounted(() => {
  window.removeEventListener("keydown", keyDown, false);
});
</script>

<style scoped lang="scss">
.login-body {
  position: relative;
  height: 100%;

  .footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 2;
    height: 10vh;
    background: #fff;
    color: #000;
  }

  .login-box {
    z-index: 2;
    position: absolute;
    top: 55%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    width: 450px;
    max-width: 100%;
    padding: 20px 0;

    .el-form-item {
      padding: 10px;
    }

    h2 {
      font-size: 24px;
      font-weight: bold;
      color: #409eff;
      // margin-bottom: 30px;
    }

    .el-button {
      padding: 17px 60px;
    }
  }

  .header {
    height: 100vh;
    position: relative;
    text-align: center;
    background: linear-gradient(60deg,
        rgba(84, 58, 183, 1) 0%,
        rgba(0, 172, 193, 1) 100%);
    color: white;
    z-index: 0;

    .logo {
      padding: 20px;
      height: 70px;
      display: flex;
      align-items: end;

      img {
        height: 100%;
        max-height: 100%;
      }

      h2 {
        padding: 0;
        margin: 0 0 0 10px;
      }
    }

    .inner-header {
      position: absolute;
      z-index: 3;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 100%;
      text-align: center;
      color: white;
    }

    .flex {
      /*Flexbox for containers*/
      display: flex;
      justify-content: center;
      align-items: center;
      text-align: center;
    }

    .waves {
      position: fixed;
      bottom: 10vh;
      left: 0;
      width: 100%;
      height: 15vh;
      margin-bottom: -7px;
      /*Fix for safari gap*/
      min-height: 100px;
      max-height: 150px;
    }

    .content {
      position: relative;
      height: 20vh;
      text-align: center;
      background-color: white;
    }

    /* Animation */

    .parallax>use {
      animation: move-forever 25s cubic-bezier(0.55, 0.5, 0.45, 0.5) infinite;
    }

    .parallax>use:nth-child(1) {
      animation-delay: -2s;
      animation-duration: 7s;
    }

    .parallax>use:nth-child(2) {
      animation-delay: -3s;
      animation-duration: 10s;
    }

    .parallax>use:nth-child(3) {
      animation-delay: -4s;
      animation-duration: 13s;
    }

    .parallax>use:nth-child(4) {
      animation-delay: -5s;
      animation-duration: 20s;
    }

    @keyframes move-forever {
      0% {
        transform: translate3d(-90px, 0, 0);
      }

      100% {
        transform: translate3d(85px, 0, 0);
      }
    }

    /*Shrinking for mobile*/
    @media (max-width: 768px) {
      .waves {
        height: 40px;
        min-height: 40px;
      }

      .content {
        height: 30vh;
      }

      h1 {
        font-size: 24px;
      }
    }
  }
}
</style>
